package com.thinker.loan.consumer.service;

import com.thinker.loan.consumer.enumeration.RiskLevel;
import com.thinker.loan.consumer.model.FraudRisk;
import com.thinker.loan.consumer.model.LoanApplicationRequest;
import org.springframework.stereotype.Service;

@Service
public class FraudDetectionService {
    public FraudRisk evaluateRisk(LoanApplicationRequest request) {
        //    **反欺诈规则链**
        //    *   检查设备指纹（同一设备24小时内申请＞3次触发警报）
        //    *   比对公安系统户籍信息（居住地址真实性验证）
        //    *   筛查国际制裁名单（OFAC/SDN List）
        String address = request.getCustomer().getAddress();
        FraudRisk fraudRisk = new FraudRisk();
        fraudRisk.setRiskLevel(RiskLevel.HIGH);
        if (containsBasicInfo(address)) {
            fraudRisk.setRiskLevel(RiskLevel.MEDIUM);
        }
        if (containsDetailedInfo(address)) {
            fraudRisk.setRiskLevel(RiskLevel.LOW);
        }
        return fraudRisk;
    }

    private boolean containsBasicInfo(String address) {
        return address != null && address.contains("省") && address.contains("市") && address.contains("区");
    }

    private boolean containsDetailedInfo(String address) {
        return containsBasicInfo(address) && address.contains("街道") && address.contains("社区") && address.contains("小区");
    }
}
